﻿using PickingGuide.Business;
using PickingGuide.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideForm
{
    public partial class FrmPlcAddresSet : Form
    {
        PlcAddresBLL plcAddresBLL = new PlcAddresBLL();
        PlcAddresModel _plcAddresModel;
        public Action RefreshUiEvent;

        public FrmPlcAddresSet()
        {
            InitializeComponent();
        }

        public FrmPlcAddresSet(PlcAddresModel model)
        {
            InitializeComponent();
            _plcAddresModel = model;
            txtArea.Text = model.Area;
            txtAddress.Text = model.Address;
            txtDataLength.Text = model.DataLength.ToString();
            txtPlcDataType.Text = model.PlcDataType.ToString();
            txtOperateType.Text = model.OperateType;
            txtSignalMark.Text = model.SignalMark;
            btnSave.Text = "修改";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_plcAddresModel == null)
            {
                var model = plcAddresBLL.GetModel(txtArea.Text, txtAddress.Text);
                if (model != null)
                {
                    MessageBox.Show("已存在相同的地址配置，不能重复！");
                    return;
                }
                _plcAddresModel = new PlcAddresModel();
                _plcAddresModel.Area = txtArea.Text;
                _plcAddresModel.Address = txtAddress.Text;
                _plcAddresModel.DataLength = int.Parse(txtDataLength.Text);
                _plcAddresModel.PlcDataType = int.Parse(txtPlcDataType.Text);
                _plcAddresModel.OperateType = txtOperateType.Text;
                _plcAddresModel.SignalMark = txtSignalMark.Text;

                var result = plcAddresBLL.Add(_plcAddresModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加地址成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加地址失败：" + result.Dsca);
            }
            else
            {
                _plcAddresModel.Area = txtArea.Text;
                _plcAddresModel.Address = txtAddress.Text;
                _plcAddresModel.DataLength = int.Parse(txtDataLength.Text);
                _plcAddresModel.PlcDataType = int.Parse(txtPlcDataType.Text);
                _plcAddresModel.OperateType = txtOperateType.Text;
                _plcAddresModel.SignalMark = txtSignalMark.Text;
                var result = plcAddresBLL.Update(_plcAddresModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改地址成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改地址失败：" + result.Dsca);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
